<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算属性-侦听器-watch</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>


    <script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
</head>
<body>
<div id="app">
    <p>
        Ask a yes/no question:
        <label>
            <input v-model="question">
            <!--question发生变化，和远程数据进行比较，写出答案-->
            <!--轮询方式间隔2s判断用户输入结束-->
        </label>
    </p>
    <p>{{ answer }}</p>
</div>



<script>

    var app = new Vue({
        el: '#app',
        data: {
            question:"",
            answer:"回答才有答案I cannot give you an answer until you ask a question!",
            message:"Hello World!"
        },
        methods:{
            getAnswer:function () {
                console.log("getAnswer");

                this.answer = 'Thinking...';//中间状态
                var vm = this;

                axios.get('https://yesno.wtf/api')
                    .then(function (response) {
                        console.log(response);
                       // vm.answer = _.capitalize(response.data.answer)
                        console.log(response.data.answer);
                        vm.answer=response.data.answer;//把云端答案放入本地 在p标签内显示
                    })
                    .catch(function (error) {
                       // vm.answer = 'Error! Could not reach the API. ' + error
                        console.log(error);

                        vm.answer="Error: "+error;
                    })
            }
        },
        watch:{
            question:function (newQ,oldQ) {
                console.log(newQ+" "+oldQ);

                this.answer="Waiting for you to stop typing...";//输入结果之后 停顿让云端和本地答案结合判断

                this.debouncedGetAnswer();//每个500ms调用一下该方法
            }
        },
        created:function () {
            this.debouncedGetAnswer = _.debounce(this.getAnswer, 500);//轮询，500ms执行一次
        },

        computed:{

        },



    });

</script>
</body>
</html>
